import { LoginManager } from '../LoginManager';
import { Pool } from '../Pool';
import http = require('http');
import queryString  = require("querystring");
import url = require('url');
import com = require('com');

export class Login
{
    constructor()
    {
        
    }

    /**服务器不关心method类型，统一解析url */
    public handle(type:string,req:http.IncomingMessage,res:http.ServerResponse):void
    {
        let send_str = '';//需要发送的内容
        let obj = url.parse(req.url);
        let query = obj.query;
        if(query)
        {
            console.log(query);
            let params = queryString.parse(query);
            console.log(params);
            let base64 = params.credential;
            let acc_pas = new Buffer(base64, 'base64').toString();
            console.log(acc_pas);
            let arr = acc_pas.split(':');

            switch(type)
            {
                case '/login':
                    //去数据库验证
                    let login = new com.LoginProto();
                    login.code = com.Code.correct;
                    login.body.result = 1;
                    login.body.acc = arr[0];
                    login.body.pwd = arr[1];
                    send_str = JSON.stringify(login);
                    LoginManager.getInstance().send(send_str);
                    Pool.getInstance().addObj(res,login.body.acc,login.body.pwd);
                    break;
                case '/register'://需要提前判断是否有这个帐号
                    if(Pool.getInstance().contains(arr[0]))//检测是否包含这个帐号
                    {
                        let obj = Pool.getInstance().getObj(arr[0]);
                        if(obj._id)//已经查询过了
                        {
                            send_str = '{code:' + com.Code.s_inquiring + '}';
                            this.responseClient(res,send_str);//直接返回该帐号已存在就行了
                            return;
                        }
                        else
                        {
                            send_str = '{code:' + com.Code.s_registering + '}';
                            this.responseClient(res,send_str);//直接返回该帐号已存在就行了
                            return;
                        }
                    }
                    let obj = Pool.getInstance().addObj(res,arr[0],arr[1]);
                    obj.pwdCoding(this.saveRegister,this,arr);
                    break;
                case '/query':
                    break;
            }
        }
        else
        {
            
        }
        // console.log('Login.send_str:',send_str);
        // res.end(send_str);
    }

    /**保存注册的信息 */
    private saveRegister(arr:any,hash:string):void
    {
        let register = new com.RegisterProto();
        register.code = com.Code.correct;
        //格式化检测
        register.body.acc = arr[0];
        register.body.pwd = hash;//生成的字符串
        register.body.email = arr[2];
        let send_str = JSON.stringify(register);
        LoginManager.getInstance().send(send_str);
        //告诉客户端，帐号创建成功,等db返回
    }

    public responseClient(res:http.ServerResponse,send_str:string):void
    {
        console.log('Login.send_str:',send_str);
        res.end(send_str);
    }
}